//
//  NSString+Extension.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/24.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "NSString+Extension.h"

@implementation NSString (Extension)

// 字符串转换成表情
+ (NSMutableString *)getEmojiStringForString:(NSString *)String {
    // 表情数组
    NSArray * emojiArray = [Emoji allEmoji];
    NSMutableString * newString = [@"" mutableCopy];
    
    for (NSInteger i = 0; i < String.length; i++) {
        NSString * regex = @"^\\s{0,2}[0-9]{1,3}$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        if ([String characterAtIndex:i] == '<' && [String characterAtIndex:i + 4] == '>') {
            NSString * centerString = [String substringWithRange:NSMakeRange(i + 1, 3)];
            if ([pred evaluateWithObject:centerString] && centerString.intValue < emojiArray.count) {
                [newString appendString:emojiArray[centerString.intValue]];
                i += 4;
                continue;
            }
        }
        [newString appendString:[String substringWithRange:NSMakeRange(i, 1)]];
    }
    return newString;
}
// 表情转换成字符串
+ (NSMutableString *)getStringForEmojiString:(NSString *)emojiString {
    // 表情数组
    NSArray * emojiArray = [Emoji allEmoji];
    NSMutableString * newString = [@"" mutableCopy];
    
    for (NSInteger i = 0; i < emojiString.length; i++) {
        if (i < emojiString.length - 1) {
            if ([emojiArray indexOfObject:[emojiString substringWithRange:NSMakeRange(i, 2)]] < emojiArray.count) {
                [newString appendString:[NSString stringWithFormat:@"<%.3ld>", [emojiArray indexOfObject:[emojiString substringWithRange:NSMakeRange(i, 2)]]]];
                i ++;
                continue;
            }
        }
        [newString appendString:[emojiString substringWithRange:NSMakeRange(i, 1)]];
    }
    return newString;
}
// 唯一名称
+ (NSString*)genaUUID {
    
    CFUUIDRef uuid_ref = CFUUIDCreate(nil);
    CFStringRef uuid_string_ref= CFUUIDCreateString(nil, uuid_ref);
    CFRelease(uuid_ref);
    NSString *uuid = [NSString stringWithString:(__bridge NSString*)uuid_string_ref];
    CFRelease(uuid_string_ref);
    return uuid;
}
// 返回字符串高度
- (CGSize)textSizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize {
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
}

@end
